package com.paipaideli.ui.mine.safety;

import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.JsutmentUtil;
import com.paipaideli.common.utils.StringUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.utils.preferrnces.FastData;
import com.paipaideli.ui.login.LoginActivity;

import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 修改手机号
 */
public class ForgetPwdActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.ed_forget_phone)
    EditText edForgetPhone;
    @BindView(R.id.et_forget_sms)
    EditText etForgetSms;
    @BindView(R.id.btn_forget_sms)
    Button btnForgetSms;
    @BindView(R.id.et_forget_onepwd)
    EditText etForgetOnepwd;
    @BindView(R.id.et_forget_twopwd)
    EditText etForgetTwopwd;
    @BindView(R.id.btn_forget_ok)
    Button btnForgetOk;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    @Override
    public int getContentView() {
        return R.layout.activity_forgetpwd;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("忘记密码");

        top_back.setOnClickListener(o -> {
            finish();
        });

        //获取验证码
        RxView.clicks(btnForgetSms).subscribe(o -> {
            getSms();
        }, throwable -> {
        });

        //忘记密码
        RxView.clicks(btnForgetOk).subscribe(o -> {
            getPwd();
        }, throwable -> {
        });
    }

    private void getSms() {
        bg_statusview.finish();
        String phone = edForgetPhone.getText().toString();
        if (!StringUtil.isEmpty(phone)) {
            if (JsutmentUtil.isMobileTrue(phone)) {
                JSONObject requestData = new JSONObject();
                try {
                    requestData.put("phone", phone);
                    requestData.put("type", "2");
                } catch (Exception e) {
                    e.getMessage();
                }
                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
                Api.getInstanceGson().sms(body)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(request -> {
                            if (request.getCode().equals("200")) {
                                addDisposable(Flowable.interval(1, TimeUnit.SECONDS)
                                        .take(60)
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .doOnComplete(() -> {
                                            btnForgetSms.setClickable(true);
                                            btnForgetSms.setText("点击再次发送");
                                        })
                                        .subscribe(aLong -> {
                                            btnForgetSms.setText("已发送" + (59 - aLong) + "秒");
                                            btnForgetSms.setClickable(false);
                                        }, throwable -> {
                                            Log.e(TAG, "error", throwable);
                                        }));
                                bg_statusview.finish();
                                ToastUtil.show("验证码发送成功！");
                            } else {
                                bg_statusview.finish();
                                ToastUtil.show(request.getMessage());
                            }
                        }, throwable -> {
                            bg_statusview.finish();
                        });
            } else {
                ToastUtil.show("请输入正确的手机号码");
            }
        } else {
            ToastUtil.show("请输入手机号码");
        }
    }

    private void getPwd() {
        String phone = edForgetPhone.getText().toString().trim();
        String yzm = etForgetSms.getText().toString().trim();
        String onePwd = etForgetOnepwd.getText().toString().trim();
        String twoPwd = etForgetTwopwd.getText().toString().trim();
        if (!StringUtil.isEmpty(phone)) {
            if (JsutmentUtil.isMobileTrue(phone)) {
                if (!StringUtil.isEmpty(yzm)) {
                    if (!StringUtil.isEmpty(onePwd)) {
                        if (!StringUtil.isEmpty(twoPwd)) {
                            if (onePwd.equals(twoPwd)) {
                                JSONObject requestData = new JSONObject();
                                try {
                                    requestData.put("phone", phone);
                                    requestData.put("smsCode", yzm);
                                    requestData.put("password", onePwd);
                                } catch (Exception e) {
                                    e.getMessage();
                                }
                                RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
                                bg_statusview.showLoading();
                                Api.getInstanceGson().forgetPwd(body)
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(request -> {
                                            if (request.getCode().equals("200")) {
                                                FastData.setUsername("");
                                                FastData.setUserPwd("");
                                                FastData.setIsLogin(false);
                                                Intent intent = new Intent(ForgetPwdActivity.this, LoginActivity.class);
                                                startActivity(intent);
                                                finish();
                                                bg_statusview.finish();
                                                ToastUtil.show(request.getMessage());
                                            } else {
                                                bg_statusview.finish();
                                                ToastUtil.show(request.getMessage());
                                            }
                                        }, throwable -> {
                                            bg_statusview.finish();
                                            Log.e(TAG, "error", throwable);
                                        });
                            } else {
                                ToastUtil.show("两次密码输入不一致，请重新输入！");
                            }
                        } else {
                            ToastUtil.show("请输入密码！");
                        }
                    } else {
                        ToastUtil.show("请输入密码！");
                    }
                } else {
                    ToastUtil.show("请输入验证码！");
                }
            } else {
                ToastUtil.show("请输入正确的手机号码");
            }
        } else {
            ToastUtil.show("请输入手机号码");
        }

    }
}
